/*
 * @lc app=leetcode.cn id=347 lang=cpp
 *
 * [347] 前 K 个高频元素
 */

// @lc code=start
class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        std::unordered_map<int, int> times;

        for (auto& num : nums)
            times[num]++;
        
        //大堆顶
        auto compare = [](const std::pair<int, int>& l, const std::pair<int, int>& r) {
            return l.second < r.second;
        };
        std::priority_queue<std::pair<int, int>, vector<std::pair<int, int>>, decltype(compare)> pq;

        for (auto it = times.begin(); it != times.end(); it++)
            pq.push(*it);

        std::vector<int> results(k);
        for (; k--;) {
            results[k] = pq.top().first;
            pq.pop();
        }

        return results;
    }
};
// @lc code=end

